"""

"""
import config as cfg
from make_dict import slot_dict
import numpy as np


def make_index(x_npy_path, slot_path, intent_path, x_txt):
    """
    数据转阿拉伯数字，返回list，每一行是[句子]，句子实际长度，[槽]，Intent标签
    :param x_npy_path: 文件路径，词向量
    :param slot_path: Slot路径，IOB格式
    :param intent_path: Intent标签路径，格式：阿拉伯数字
    :param x_txt: 原始句子，未使用，这里用slot的长度表示了句子的实际长度
    :return: train_data 格式 [ [[句子]，句子实际长度，[Slot]，Intent标签], ...]
    """
    train_data = []

    train_x = np.load(x_npy_path, allow_pickle=True)
    true_len = []  # 每句话的实际长度
    train_slot = []  # 转成数字后的slot
    train_intent = np.load(intent_path, allow_pickle=True)
    num_unknown = 0

    with open(slot_path) as f:
        for line in f.readlines():
            line = line.strip()
            temp_slot = []
            for slot in line.split(' '):
                try:
                    temp_slot.append(slot_dict[slot])
                except KeyError:
                    # 如果slot在字典中找不到，用O对应的数字1代替，num_unknown+1
                    temp_slot.append(slot_dict['O'])
                    num_unknown += 1
            # 以<PAD>开头
            temp_slot.insert(0, slot_dict['<PAD>'])
            true_len.append(len(temp_slot))

            if len(temp_slot) < 30:
                # 长度小于30的，以<PAD>填充
                temp_slot.extend([slot_dict['<PAD>']] * (30 - len(temp_slot)))  # slot padding
            train_slot.append(temp_slot)

        print('num_unknown', num_unknown)

    print(len(train_x), len(train_slot), len(true_len), len(train_intent))
    assert len(train_x) == len(true_len) == len(train_slot) == len(train_intent)

    for i in range(len(train_x)):
        # 格式：句子，句子实际长度，槽，Intent标签
        train_data.append([train_x[i], true_len[i], train_slot[i], train_intent[i]])
    return train_data


def getData(fold):
    """
    加载数据
    :param fold: 加载第几折的数据
    :return: train_data, dev_data [[句子]，句子实际长度，[Slot]，Intent标签]
    """
    # 训练集
    train_data = make_index(cfg.x_train_npy.replace('0', str(fold)), cfg.slot_train.replace('0', str(fold)),
                            cfg.intent_train_npy.replace('0', str(fold)), cfg.x_train_txt.replace('0', str(fold)))
    # 验证集/开发集
    dev_data = make_index(cfg.x_dev_npy.replace('0', str(fold)), cfg.slot_dev.replace('0', str(fold)),
                          cfg.intent_dev_npy.replace('0', str(fold)), cfg.x_dev_txt.replace('0', str(fold)))

    return train_data, dev_data




# 下面的代码互换key和value. slot_dict: key是槽名称，value是数字
index2slot_dict = {v: k for k, v in slot_dict.items()}
